package com.cnsunrun.testing;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.aitian.tomorrow.R;
import com.cnsunrun.base.Config;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * 启动页(引导页)
 * 启动图默认名称start_.png 建议配置activity主题背景,防止白屏
 * <style name="SplashActivityStyle" parent="AppBaseTheme">
 * <item name="android:windowBackground">@drawable/start_</item>
 * </style>
 * 引导图默认路径 assets/guide/
 */
public class SplashActivity extends Activity {
    private static final String TAG = "SplashActivity";
    private static final String START_NUM = "START_NUM";//App启动次数
    private static final String SP_NAME = "START_PAGE";//启动次数配置文件名
    private static final String START_IMG_DIR = "guide";//引导图存放目录
    long DEALY_TIME = 2000;//启动页停留时间
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        viewPager= (ViewPager) findViewById(R.id.viewPager);
        String startImages[] = getStartImg();
        if (startNumber() <= 0 && startImages != null) {
            viewPager.setVisibility(View.VISIBLE);
            viewPager.setAdapter(new InnerImageAdapter(this, Arrays.asList(startImages)));
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if (position == viewPager.getAdapter().getCount() - 1) {
                        pageFinish(DEALY_TIME);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        } else {
            pageFinish(DEALY_TIME);
        }
    }

    /**
     *
     */
    public void pageFinish(long dealyTime) {
        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, dealyTime);

    }
    String[] getStartImg() {
        try {
            String[] bgs = getAssets().list(Config.START_IMG);
            for (int i = 0; i < bgs.length; i++) {
                bgs[i] = START_IMG_DIR + "/" + bgs[i];
            }
            return bgs;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int startNumber() {
        int startNumber = 0;
        SharedPreferences sharedPreferences = getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        startNumber = sharedPreferences.getInt(START_NUM, startNumber);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(START_NUM, startNumber + 1);
        editor.commit();
        return startNumber;
    }

    /**
     * 引导页适配器
     */
    public class InnerImageAdapter extends PagerAdapter {
        protected List<String> imageIdList;
        protected Context mContext;

        public InnerImageAdapter(Context mContext, List<String> imageIdList) {
            this.imageIdList = imageIdList;
            this.mContext = mContext;
        }

        @Override
        public final Object instantiateItem(ViewGroup container, int position) {
            String mItem = imageIdList.get(position);
            ImageView imageView = new ImageView(mContext);
            try {
                Bitmap bm = BitmapFactory.decodeStream(mContext.getAssets()
                        .open(mItem));
                imageView.setImageBitmap(bm);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(imageView, layoutParams);
            return imageView;
        }

        @Override
        public final void destroyItem(ViewGroup container, int position,
                                      Object object) {
            View view = (View) object;
            container.removeView(view);
        }

        @Override
        public int getCount() {
            return imageIdList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
